Lecture 3 - Practice
Q1: Parent & Grandparent Rules
اعمل قاعدة بيانات للعيلة دي:
- Aly is the father of Hany.
- Saly is the mother of Hany.
- Samy is the father of Aly.
وبعدين اعمل الـ rules دي.
- X is a parent of Y if X is the father of Y or X is the mother of Y.
- X is a grandparent of Y if X is a parent of Z and Z is a parent of Y.
واكتب الاستعلامات:
- Who is Hany's parent?
- Who is Aly's father?
- Who is Aly's grandparent?
% Facts
father(aly, hany).
mother(saly, hany).
father(samy, aly).
% Rules
parent(X, Y) :- father(X, Y).
parent(X, Y) :- mother(X, Y).
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
**Queries:** ```prolog ?- parent(X, hany). % X = aly ; X = saly ?- father(Y, aly). % Y = samy ?- grandparent(X, hany). % X = samy
Q2: Family Tree Facts
اعمل قاعدة بيانات لشجرة العيلة دي :
- Hany is a parent of Samy.
- Saly is a parent of Samy.
- Saly is a parent of Lila.
- Samy is a parent of Aly.
- Samy is a parent of Dena.
- Dena is a parent of Tamer.
- Hany, Samy, Aly, Tamer are male.
- Saly, Dena, Lila are female.
parent(hany, samy).
parent(saly, samy).
parent(saly, lila).
parent(samy, aly).
parent(samy, dena).
parent(dena, tamer).
male(hany).
male(samy).
male(aly).
male(tamer).
female(saly).
female(dena).
female(lila).
Q3: Family Tree Rules
من قاعدة البيانات بتاعة شجرة العيلة (سؤال 2)، اعمل الـ rules دي:
- X is the father of Y if X is a parent of Y and X is male.
- X is the mother of Y if X is a parent of Y and X is female.
- X is a sibling of Y if they both have the same parent Z.
- Y is an offspring of X if X is a parent of Y.
- X is a sister of Y if X is a sibling of Y and X is female.
father(X, Y) :- parent(X, Y), male(X).
mother(X, Y) :- parent(X, Y), female(X).
sibling(X, Y) :- parent(Z, X), parent(Z, Y).
offspring(Y, X) :- parent(X, Y).
sister(X, Y) :- sibling(X, Y), female(X).
Q4: Family Tree Queries
من قاعدة البيانات بتاعة شجرة العيلة (سؤال 2)، اكتب الاستعلامات دي:
- Is Samy a parent of Dena?
- Is Aly a parent of Dena?
- Who is Lila's parent?
- Who is a parent of Aly and a parent of Dena?
- List all parent relationships.
- Who is Tamer's parent?
- Who is Dena's child?
- Who are the male offspring of Hany?
- Who has a sister?
- Who is Dena's grandparent?
?- parent(samy, dena). % true
?- parent(aly, dena). % false
?- parent(X, lila). % X = saly
?- parent(X, aly), parent(X, dena). % X = samy
?- parent(X, Y). % lists all
?- parent(X, tamer). % X = dena
?- parent(dena, X). % X = tamer
?- parent(hany, X), male(X). % X = samy
?- parent(Z, X), parent(Z, Y), female(X). % sister queries
?- parent(X, Y), parent(Y, dena). % X = hany, Y = samy ; X = saly, Y = samy ;
Q5: Student Age Database
اعمل قاعدة بيانات للطلاب دي using the predicate اstudent(Name, Sex, Age):
| Name | Sex | Age |
|---|---|---|
| Ahmed | male | 30 |
| Hany | male | 25 |
| Rwan | female | 20 |
| Mona | female | 18 |
وبعدين اعمل rule:
- X is older than Y if age of X is greater than age of Y.
واكتب الاستعلامات:
- What is Hany's age?
- Who is male?
- List names with age < 30 and they are female.
- Find students older than Mona.
- Find students with age less than Ahmed.
- Find students younger than Ahmed and older than Mona.
% Facts
student(ahmed, male, 30).
student(hany, male, 25).
student(rwan, female, 20).
student(mona, female, 18).
% Rule
older(X, Y) :- student(X, _, AgeX), student(Y, _, AgeY), AgeX > AgeY.
Queries:
?- student(hany, _, Age). % Age = 25
?- student(Name, male, _). % Name = ahmed ; Name = hany
?- student(Name, female, Age), Age < 30. % Name = rwan ; Name = mona
?- older(X, mona). % X = ahmed ; X = hany ; X = rwan
?- student(X, _, AgeX), student(ahmed, _, AgeA), AgeX < AgeA.
% X = hany ; X = rwan ; X = mona
?- student(X, _, Age), student(ahmed, _, AgeA), student(mona, _, AgeM), Age < AgeA, Age > AgeM.
% X = hany ; X = rwan
Q6: son/child Rule
اعمل قاعدة بيانات:
- Samy is male.
- Hany is male.
- Samy is a child of Hany.
وبعدين اعمل rule:
- X is a son of Y if X is male and X is a child of Y.
واكتب الاستعلامات:
- Who is Samy the son of?
- Who is male? (اعرض كل الإجابات)
- Is Samy the son of someone? (استخدم anonymous variable
_)
% Facts
male(samy).
male(hany).
child(samy, hany).
% Rule
son(X, Y) :- male(X), child(X, Y).
Queries:
?- son(samy, X). % X = hany
?- male(X). % X = samy ; X = hany
?- son(samy, _). % true
Q7: Recursive above (Blocks)1.
اعمل قاعدة بيانات لـ 4 blocks فوق بعضها:
1.1. Block a is on block b.
1.2. Block b is on block c.
1.3. Block c is on block d.
وبعدين اعمل recursive rule:
- X is above Y if X is directly on Y.
- X is above Y if X is on Z and Z is above Y.
واكتب الاستعلامات:
- Is block b on block c?
- Is block a on block d?
- Is block a above block d?
- What blocks are above block c?
% Facts
on(a, b).
on(b, c).
on(c, d).
% Recursive Rules
above(X, Y) :- on(X, Y).
above(X, Y) :- on(X, Z), above(Z, Y).
Queries:
?- on(b, c). % true
?- on(a, d). % false
?- above(a, d). % true
?- above(X, c). % X = b ; X = a
Q8: Recursive predecessor
اعمل قاعدة بيانات:
- a is a parent of b.
- b is a parent of c.
- c is a parent of d.
- d is a parent of e.
وبعدين اعمل recursive rule:
- X is a predecessor of Z if X is a parent of Z.
- X is a predecessor of Z if X is a parent of Y and Y is a predecessor of Z.
واكتب الاستعلامات:
- Is a a parent of b?
- Is a a predecessor of b?
- Is a a predecessor of e?
- Who are the predecessors of d?
- Who are the descendants of a?
% Facts
parent(a, b).
parent(b, c).
parent(c, d).
parent(d, e).
% Recursive Rules
predecessor(X, Z) :- parent(X, Z).
predecessor(X, Z) :- parent(X, Y), predecessor(Y, Z).
Queries:
?- parent(a, b). % true
?- predecessor(a, b). % true
?- predecessor(a, e). % true
?- predecessor(X, d). % X = c ; X = b ; X = a
?- predecessor(a, X). % X = b ; X = c ; X = d ; X = e
Q9: Recursive is_bigger
اعمل قاعدة بيانات بالحقائق دي:
- An elephant is bigger than a horse.
- A horse is bigger than a donkey.
- A donkey is bigger than a dog.
- A donkey is bigger than a monkey.
وبعدين اعمل recursive rule:
- X is bigger than Y if X is directly bigger than Y.
- X is bigger than Y if X is bigger than Z and Z is bigger than Y.
واكتب الاستعلامات:
- Is an elephant bigger than a monkey?
- What is bigger than a donkey?
- What is a horse bigger than?
- What is bigger than a dog and that thing is also bigger than a monkey?
% Facts
bigger(elephant, horse).
bigger(horse, donkey).
bigger(donkey, dog).
bigger(donkey, monkey).
% Recursive Rules
is_bigger(X, Y) :- bigger(X, Y).
is_bigger(X, Y) :- bigger(X, Z), is_bigger(Z, Y).
Queries:
?- is_bigger(elephant, monkey). % true
?- is_bigger(X, donkey). % X = horse ; X = elephant
?- is_bigger(horse, X). % X = donkey ; X = dog ; X = monkey
?- is_bigger(elephant, X), is_bigger(X, donkey).
% X = horse
Q10: Graph Route with Distances
اعمل قاعدة بيانات للـ graph دي using the predicate link(Node1, Node2, Distance):
- Node a is connected to b with distance 2.
- Node a is connected to c with distance 3.
- Node b is connected to d with distance 4.
- Node b is connected to e with distance 5.
- Node c is connected to f with distance 6.
- Node c is connected to g with distance 7.
graph TD A["a"] A -->|"2"| B["b"] A -->|"3"| C["c"] B -->|"4"| D["d"] B -->|"5"| E["e"] C -->|"6"| F["f"] C -->|"7"| G["g"]
وبعدين اعمل recursive rule:
- X can reach Y with distance D if there is a direct link between them.
- X can reach Y with distance D if X can reach Z with distance D1 and Z can reach Y with distance D2 and D = D1 + D2.
واكتب الاستعلامات:
- What nodes are directly connected to node b and their distance?
- Which nodes connect to node c and their distance?
- Which other nodes can you reach when starting at node a and their distances?
- Which nodes can be found between node a and node e, and their distances from a and to e?
% Facts
link(a, b, 2).
link(a, c, 3).
link(b, d, 4).
link(b, e, 5).
link(c, f, 6).
link(c, g, 7).
% Recursive Rules
route(X, Y, D) :- link(X, Y, D).
route(X, Y, D) :- link(X, Z, D1), route(Z, Y, D2), D is D1 + D2.
Queries:
?- link(b, X, D). % X = d, D = 4 ; X = e, D = 5
?- link(X, c, D). % X = a, D = 3
?- route(a, X, D). % X = b, D = 2 ; X = c, D = 3 ;
% X = d, D = 6 ; X = e, D = 7 ;
% X = f, D = 9 ; X = g, D = 10
?- route(a, X, D1), route(X, e, D2). % X = b, D1 = 2, D2 = 5
Q11: Flights (Faster & Travel)
اعمل قاعدة بيانات للرحلات الجوية دي:
| Flight | From | To | Duration (hr) |
|---|---|---|---|
| BA1 | Cairo | Rome | 3.5 |
| AL2 | Rome | Paris | 2.5 |
| BA6 | Cairo | London | 6.2 |
| AF1 | Paris | Brussels | 0.6 |
وبعدين اعمل rules:
- Flight N is faster than flight M if duration of N < duration of M.
- You can travel from X to Y with total duration D if there is a direct flight from X to Y with duration D.
- You can travel from X to Y with total duration D if there is a flight from X to Z with duration D1 and you can travel from Z to Y with duration D2 and D = D1 + D2.
واكتب الاستعلامات:
- Which cities can be reached by a single flight from Cairo?
- How long does flight AL2 take?
- From which cities can you fly to London (by a single flight)?
- Which flight numbers are faster than BA1?
- Which flight numbers are slower than AL2?
- Which cities can be reached by some sequence of flights from Cairo?
- From which cities can one travel to Paris by some sequence of flights?
% Facts
flight(ba1, cairo, rome, 3.5).
flight(al2, rome, paris, 2.5).
flight(ba6, cairo, london, 6.2).
flight(af1, paris, brussels, 0.6).
% Rules
faster(N, M) :- flight(N, _, _, D1), flight(M, _, _, D2), D1 < D2.
travel(X, Y, D) :- flight(_, X, Y, D).
travel(X, Y, D) :- flight(_, X, Z, D1), travel(Z, Y, D2), D is D1 + D2.
Queries:
?- flight(_, cairo, City, _). % City = rome ; City = london
?- flight(al2, _, _, Duration). % Duration = 2.5
?- flight(_, From, london, _). % From = cairo
?- faster(F, ba1). % F = al2 ; F = af1
?- flight(F, _, _, D), flight(al2, _, _, D2), D > D2.
% F = ba1, D = 3.5 ; F = ba6, D = 6.2
?- travel(cairo, City, _). % City = rome ; City = london ;
% City = paris ; City = brussels
?- travel(City, paris, _). % City = rome ; City = cairo
Q12: Reigns Database
اعمل قاعدة بيانات لفترات حكم الأمراء دي:
| Prince Name | Reign year | To year |
|---|---|---|
| Aly | 200 | 220 |
| Samy | 221 | 230 |
| Hany | 231 | 245 |
| Lotfy | 246 | 260 |
وبعدين اعمل rule: X was a prince during year Y if X reigned between years A and B, and Y is between A and B inclusive.
واكتب الاستعلامات:
- Was Samy a prince in year 225?
- Was Aly a prince in year 1979?
- Which prince(s) were on the throne in year 240?
% Facts
reigns(aly, 200, 220).
reigns(samy, 221, 230).
reigns(hany, 231, 245).
reigns(lotfy, 246, 260).
% Rule
prince(X, Y) :- reigns(X, A, B), Y >= A, Y =< B.
Queries:
?- prince(samy, 225). % true
?- prince(aly, 1979). % false
?- prince(X, 240). % X = hany